package com.atzxh.collection.list;

import java.util.Vector;

/**
 * @author 小红同学
 * @create 2022-10-13-9:42
 * @description
 */
@SuppressWarnings({"all"})
public class Vector_Source {
    public static void main(String[] args) {

        //1. 无参构造器
        Vector vector = new Vector();

        for (int i = 0; i < 10; i++) {
            vector.add(i);
        }

        vector.add(100);

        Vector vector1 = new Vector(8);



        // 源码
        // 1. 无参 new Vector() 底层
//            public Vector() {
//                this(10);
//            }
        //    有参
//            public Vector(int initialCapacity) {
//                this(initialCapacity, 0);
//            }
//            public Vector( int initialCapacity, int capacityIncrement){
    //            super();
    //            if (initialCapacity < 0)
    //                throw new IllegalArgumentException("Illegal Capacity: " +
    //                        initialCapacity);
    //            this.elementData = new Object[initialCapacity];
    //            this.capacityIncrement = capacityIncrement;
//            }
        // 2. add()
//            public synchronized boolean add(E e) {
//                modCount++;
//                ensureCapacityHelper(elementCount + 1);
//                elementData[elementCount++] = e;
//                return true;
//            }
        //    2.1 ensureCapacityHelper(int minCapacity)  判断用不用扩容
        //          扩容条件 -->   minCapacity - elementData.length > 0
//            private void ensureCapacityHelper(int minCapacity) {
//                // overflow-conscious code
//                if (minCapacity - elementData.length > 0)
//                    grow(minCapacity);
//            }
        //   2.2  grow(int minCapacity)  扩容方法
        //        扩容机制 ：当 minCapacity - elementData.length > 0 ,
        //                  容量扩大二倍
//            private void grow(int minCapacity) {
//                // overflow-conscious code
//                int oldCapacity = elementData.length;
//                int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
//                        capacityIncrement : oldCapacity);
//                if (newCapacity - minCapacity < 0)
//                    newCapacity = minCapacity;
//                if (newCapacity - MAX_ARRAY_SIZE > 0)
//                    newCapacity = hugeCapacity(minCapacity);
//                elementData = Arrays.copyOf(elementData, newCapacity);
//            }

    }
}
